﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class CalendarQuery_SectionTree : System.Web.UI.Page {
    private SectionServices sectionService = new SectionServices();

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            this.AddSectionTree();
        }
    }

    /// <summary>
    /// 查找所有部门填充树节点
    /// </summary>
    protected void AddSectionTree() {
        List<Section> rootSections = sectionService.findByBaseSectionId(0);
        foreach(Section section in rootSections) {
            TreeNode root = new TreeNode();
            root.Text = section.SectionName;
            root.Value = section.SectionId.ToString();
            root.ImageUrl = "~/images/Sections.png";
            Recursion(root, section.SectionId);
            this.TreeView1.Nodes.Add(root);
        }
    }

    /// <summary>
    /// 树节点递归填充
    /// </summary>
    /// <param name="node"></param>
    /// <param name="bsid"></param>
    protected void Recursion(TreeNode node, int bsid) {
        List<Section> list = sectionService.findByBaseSectionId(bsid);
        if(list != null && list.Count > 0) {
            foreach(Section section in list) {
                TreeNode snode = new TreeNode();
                snode.Text = section.SectionName;
                snode.Value = section.SectionId.ToString();
                snode.ImageUrl = "~/images/Sections.png";
                Recursion(snode, section.SectionId);
                node.ChildNodes.Add(snode);
            }
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) {
        int bsid = int.Parse(TreeView1.SelectedNode.Value);
        for(int i = 0; i < TreeView1.SelectedNode.ChildNodes.Count; i++) {
            if(TreeView1.SelectedNode.ChildNodes[i].ImageUrl.Equals("~/images/node_user.png")) {
                TreeView1.SelectedNode.ChildNodes.Remove(TreeView1.SelectedNode.ChildNodes[i]);
                i--; 
            }
        }
        UserManager um = new UserManager();
        List<User> list = um.findBySectionId(bsid);
        if(list != null && list.Count > 0) {
            foreach(User user in list) {
                TreeNode node = new TreeNode();
                node.Text = user.UserName;
                node.Target = "SectionMainFrame";
                node.Value = user.LoginName;
                node.NavigateUrl = "EmpCalendarShow.aspx?u_ln=" + user.LoginName;
                node.ImageUrl = "~/images/node_user.png";
                TreeView1.SelectedNode.ChildNodes.AddAt(0, node);
            }
        }
    }
}
